package servlets.admin;

import dao.BuyJpaController;
import dao.UserJpaController;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pojo.Buy;
import pojo.User;


public class ServletViewUsers extends HttpServlet {

   
    UserJpaController userCont = new UserJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
    BuyJpaController bCont = new BuyJpaController(Persistence.createEntityManagerFactory("ServletProjectPU"));
    
    List<User> userList = new ArrayList();
    List<Buy> buys = new ArrayList();
    
    
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            userList = userCont.findUserEntities();
            
            out.println("<tr><th>Username</th> <th>Phone</th> <th>Email</th> <th>Credit</th> <th>Status</th>  <th>Change status</th> <th>View history</th>  </tr>");
            
            for(User u : userList){
                
                out.println("<tr> <td>"+u.getUsername()+"</td> <td>"+u.getMobile()+"</td> <td>"+u.getEmail()+"</td> <td>"+u.getCredit()+"</td>");
                if(u.getActive() == 1){
                    out.print("  <td>Active</td>");
                    out.print(" <td><a href='ServletActivateDeactivate?uid="+u.getId()+"' onclick='return confirm(\"Are you sure you want to deactivate user?\");'>Deactivate</a></td>");
                }
                else{
                    out.print("  <td>Deactive</td>");
                    out.print(" <td><a href='ServletActivateDeactivate?uid="+u.getId()+"' onclick='return confirm(\"Are you sure you want to activate user?\");'>Activate</a></td>");
                }
                
                 
                 
                out.println("<td><div id='my_popup"+u.getId()+"' style='background-color:#fff;display:none;padding:.3em;position:fixed; border:1px dotted gray; width:600px;  left:50%; top:50%; z-index:999; margin-left:-300px; margin-top:-160px;'><table border='1' style='width:100%;margin:20px auto 0px;background-color:#fff;'> "
                        + ""
                        + "<tr><th>Product</th> <th>Price</th><th>Quantity</th> <th>Time of transaction</th> </tr>");
                boolean f = true;
                if(u.getBuyList().isEmpty())
                {
                    
                    List<Buy> temp = new ArrayList();
                    temp.add(new Buy());
                    buys = temp;
                    f = false;
                }
                else
                {
                    buys = u.getBuyList();
                    f = true;
                }
                    for (Buy buy : buys)
                    {
                        if(f)
                        out.print("<tr><td>" + buy.getProductId().getName() + "</td> <td>" + buy.getProductId().getPrice() + "</td> <td>" + buy.getAmount() + "</td> <td>" + buy.getTime() + "</td> </tr>");
                        else
                        {
                        out.print("<tr><td>N/A</td> <td>N/A</td> <td>N/A</td> <td>N/A</td> </tr>");    
                        }
                    }
                        out.print( " </table></div>");
                out.print("<a href='#' onClick=\" $('#dim_wrapper').toggle(); $('#dim_wrapper').animate({'opacity':0.6}, 700 ); show_popup('my_popup"+u.getId()+"');  return false;\" >view</a> </td>  </tr>)");
            }
            
            
            
            
            out.println("<script>function show_popup(id) {\n" +
"			if (document.getElementById){\n" +
"				obj = document.getElementById(id);\n" +
"				if (obj.style.display == \"none\") {\n" +
"					obj.style.display = \"\";\n" +
"				}\n" +
"			}\n" +
"		}\n" +
"		function hide_popup(id){\n" +
"			if (document.getElementById){\n" +
"				obj = document.getElementById(id);\n" +
"				if (obj.style.display == \"\"){\n" +
"					obj.style.display = \"none\";\n" +
"				}\n" +
"			}\n" +
"		}</script>");
            
            
            /* TODO output your page here. You may use following sample code. */
         
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
